<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="激活码" prop="activationCode">
        <el-input v-model="queryParams.activationCode" placeholder="请输入激活码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="序列号" prop="serialNumber">
        <el-input v-model="queryParams.serialNumber" placeholder="请输入序列号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="生成时间" prop="generationTime">
        <el-date-picker clearable v-model="queryParams.generationTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择生成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="激活时间" prop="activationTime">
        <el-date-picker clearable v-model="queryParams.activationTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择激活时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="激活人员" prop="activatedBy">
        <el-input v-model="queryParams.activatedBy" placeholder="请输入激活人员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属公司" prop="company">
        <el-select v-model="queryParams.company" placeholder="请选择所属公司" clearable>
          <el-option v-for="dict in dict.type.compny_name" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属产品" prop="product">
        <el-select v-model="queryParams.product" placeholder="请选择所属产品" clearable>
          <el-option v-for="dict in dict.type.product_name" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="生效周期" prop="youxiaoqi">
        <el-select v-model="queryParams.youxiaoqi" placeholder="请选择生效周期" clearable>
          <el-option v-for="dict in dict.type.youxiao_qi" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请等待期" prop="activationPeriod">
        <el-select v-model="queryParams.activationPeriod" placeholder="请选择申请等待期" clearable>
          <el-option v-for="dict in dict.type.expiry_date" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="有效期" prop="expiryDate">
        <el-select v-model="queryParams.expiryDate" placeholder="请选择有效期" clearable>
          <el-option v-for="dict in dict.type.activation_period" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="激活状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择激活状态" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="number">
        <el-select v-model="queryParams.number" placeholder="请选择产权类型" clearable>
          <el-option v-for="dict in dict.type.pro_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:ActivationCodes:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:ActivationCodes:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:ActivationCodes:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:ActivationCodes:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ActivationCodesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="20" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="激活码" align="center" width="150" prop="activationCode" />
      <el-table-column label="序列号" align="center" width="200" prop="serialNumber" />
      <el-table-column label="生成时间" align="center" prop="generationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.generationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" align="center" prop="company">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.compny_name" :value="scope.row.company" />
        </template>
      </el-table-column>
      <el-table-column label="所属产品" align="center" prop="product">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_name" :value="scope.row.product" />
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="youxiaoqi">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.youxiao_qi" :value="scope.row.youxiaoqi" />
        </template>
      </el-table-column>
      <el-table-column label="申请等待期" align="center" prop="activationPeriod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.expiry_date" :value="scope.row.activationPeriod" />
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="expiryDate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activation_period" :value="scope.row.expiryDate" />
        </template>
      </el-table-column>
      <el-table-column label="激活时间" align="center" prop="activationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="激活人员" align="center" prop="activatedBy" />
      <el-table-column label="产品类型" align="center" prop="number">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_type" :value="scope.row.number" />
        </template>
      </el-table-column>
      <el-table-column label="激活状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ActivationCodes:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:ActivationCodes:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改激活码增删改查对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="激活码" prop="activationCode">
          <el-input :disabled="true" v-model="form.activationCode" placeholder="请输入激活码" />
        </el-form-item>
        <el-form-item label="序列号" prop="serialNumber">
          <el-input :disabled="true" v-model="form.serialNumber" placeholder="请输入序列号" />
        </el-form-item> -->
        <el-form-item label="生成时间" prop="generationTime">
          <el-date-picker clearable v-model="form.generationTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择生成时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="激活时间" prop="activationTime">
          <el-date-picker clearable
                          v-model="form.activationTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择激活时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="激活人员" prop="activatedBy">
          <el-input v-model="form.activatedBy" placeholder="请输入激活人员" />
        </el-form-item> -->
        <el-form-item label="所属公司" prop="company">
          <el-select v-model="form.company" placeholder="请选择所属公司">
            <el-option v-for="dict in dict.type.compny_name" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属产品" prop="product">
          <el-select v-model="form.product" placeholder="请选择所属产品">
            <el-option v-for="dict in dict.type.product_name" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生效周期" prop="youxiaoqi">
          <el-select v-model="form.youxiaoqi" placeholder="请选择生效周期">
            <el-option v-for="dict in dict.type.youxiao_qi" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请等待期" prop="activationPeriod">
          <el-select v-model="form.activationPeriod" placeholder="请选择申请等待期">
            <el-option v-for="dict in dict.type.expiry_date" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效期" prop="expiryDate">
          <el-select v-model="form.expiryDate" placeholder="请选择有效期">
            <el-option v-for="dict in dict.type.activation_period" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="激活状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="产品类型" prop="number">
          <el-select v-model="form.number" placeholder="请选择产品类型">
            <el-option v-for="dict in dict.type.pro_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生成条数" prop="numbers">
          <el-input v-model="form.numbers" placeholder="请输入生成条数" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivationCodes, getActivationCodes, delActivationCodes, addActivationCodes, updateActivationCodes } from "@/api/system/ActivationCodes";

export default {
  name: "ActivationCodes",
  dicts: ['expiry_date', 'sys_yes_no', 'compny_name', 'product_name', 'activation_period', 'pro_type','youxiao_qi'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 激活码增删改查表格数据
      ActivationCodesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activationCode: null,
        serialNumber: null,
        generationTime: null,
        activationTime: null,
        activatedBy: null,
        company: null,
        product: null,
        activationPeriod: null,
        expiryDate: null,
        status: null,
        number: null,
        numbers: 0,
        youxiaoqi: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        activationCode: [
          { required: true, message: "激活码不能为空", trigger: "blur" }
        ],
        serialNumber: [
          { required: true, message: "序列号不能为空", trigger: "blur" }
        ],
        generationTime: [
          { required: true, message: "生成时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询激活码增删改查列表 */
    getList() {
      this.loading = true;
      listActivationCodes(this.queryParams).then(response => {
        this.ActivationCodesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        activationCode: null,
        serialNumber: null,
        generationTime: null,
        activationTime: null,
        activatedBy: null,
        company: null,
        product: null,
        activationPeriod: null,
        expiryDate: null,
        status: null,
        number: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加激活码";
      //fuzhi
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivationCodes(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改激活码";
      });
    },
    getRandomCode(count) {
      const codes = new Set();
      const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

      while (codes.size < count) {
        let code = '';
        for (let i = 0; i < 6; i++) {
          code += characters.charAt(Math.floor(Math.random() * characters.length));
        }
        codes.add(code);
      }

      return Array.from(codes);
    },
    generateAllCodes() {
      const codes = [];
      for (let i = 1; i <= 100000; i++) {
        codes.push(i.toString().padStart(6, '0'));
      }
      return codes;
    },
    /** 提交按钮 */
    submitForm() {

      let me = this
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivationCodes(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
              addActivationCodes(this.form).then(response => {

                  this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();

              });

          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除激活码增删改查编号为"' + ids + '"的数据项？').then(function () {
        return delActivationCodes(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ActivationCodes/export', {
        ...this.queryParams
      }, `ActivationCodes_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
